//
//  PostResult.m
//  parkour-photomap-ib
//
//  Created by Ed Mancebo on 4/8/11.
//  Copyright 2011 Personal. All rights reserved.
//

#import "PostResult.h"


@implementation PostResult

@synthesize isOK, errorMessage, rawResponse, photoId;

- (id) init {
	self = [super init];
	if (self) {
		inPhotoID = NO;
	}
	return self;
}

- (void) populateFromXmlResponse:(NSString*)response {
	NSData* xmlData = [response dataUsingEncoding:NSUTF8StringEncoding];
	NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
	parser.delegate = self;
	[parser parse];
	[parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
	if ([elementName isEqualToString:@"rsp"]) {
		NSString *status = [attributeDict objectForKey:@"stat"];
		self.isOK = [status isEqualToString:@"ok"];
	}
	else if ([elementName isEqualToString:@"err"]) {
		self.errorMessage = [attributeDict objectForKey:@"msg"];
	}
	else if ([elementName isEqualToString:@"photoid"]) {
		inPhotoID = YES;
	}
	
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
	if (inPhotoID) {
		self.photoId = string;
		inPhotoID = NO;
	}
}

- (NSString*) description {
	return [NSString stringWithFormat:@"stat:%d err:%@ photoId:%@", isOK, errorMessage, photoId];
}


@end
